#include <string>
#include <iostream>
using namespace std;
#include "TreeNode.h"
#include "DataElementCharArray.h"
#include "DataElementDouble.h"
#include "DataElementInt.h"
TreeNode::TreeNode(const string &strType, const string &strValue, TreeNode* parentLink, unsigned numChildrenLinks)
{
	dataField = CreateDataField(strType);
	dataField->SetValue(strValue);
	numOfChildrenLinks = numChildrenLinks;
	TreeNode** links = new TreeNode*[numOfChildrenLinks];
	for (int i = 0; i < numOfChildrenLinks; i++)
	{
		childrenLinks[i] = NULL;
	}
	this->parentLink = parentLink;
}
TreeNode::~TreeNode()
{

	delete [] childrenLinks;	// Delete recursively, then delete array of links
}
bool TreeNode::SetLink(TreeNode* lnk)
{
	if (numOfUsedChildrenLinks < numOfChildrenLinks)
	{
		childrenLinks[numOfUsedChildrenLinks-1] = lnk;
		numOfUsedChildrenLinks++;
		return true;
	}
	else
	{
		return false;
	}
}
string TreeNode::GetData()
{
	return dataField->GetValue();
}
void TreeNode::SetValue(const string& value)
{
	dataField->SetValue(value);
}
DataElementBase* TreeNode::CreateDataField(const string& type) const
{
	if (type == "int")
	{
		return (new DataElementInt());
	}
	else if (type == "double")
	{
		return (new DataElementDouble());
	}
	else if (type == "char array")
	{
		return (new DataElementCharArray());
	}
	else
	{
		// error
		cout << "Non-existent type of parameter is found. Terminating." << endl;
		cin.get();
		exit(-1);
	}
}